<?php

class AdminPlayerController extends AdminController
{
  use ApiTrait;

  public function listAction()
  {
    $playerModel = $this->getModel('player');
    $view = $this->getView();
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'opengift' => trim($this->req->getQuery('opengift', '')),
    ];
    $view = $this->getView();
    $list = $playerModel->getPlayersList($query, $page, $rows);
    $count = $playerModel->getPlayersCount($query);
    $view->assign([
      'query' => $query,
      'list' => $list, 'count' => $count,
      'page' => $page, 'rows' => $rows
    ]);
    $view->display('admin/player/list.phtml');
  }

  public function exportOpenGiftAction()
  {
    header('Content-type:application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename='. date('Y-m-d_G-i-s', time()) . '_开店礼品.xls');
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf8" />';
    echo '<table width="200" border="1">';
    echo '<tr><td>' . join('</td><td>', [
      '微信名', '姓名', '电话', '地址', '礼品', '申请时间'
    ]) . '</td></tr>';
    $playerModel = $this->getModel('player');
    $query = [
      'opengift' => 1,
    ];
    $rows = 100;
    $page = 1;
    do {
      foreach (($list = $playerModel->getPlayersList($query, $page++, $rows)) as $player) {
        echo '<tr><td>' . join('</td><td style="vnd.ms-excel.numberformat:@ ">', [
          $player->nickname, $player->realname, $player->phone,
          $player->address, '开店礼品', date('Y-m-d G:i:s', $player->created)
        ]) . '</td></tr>';
      }
    } while( count($list) == $rows);
    echo '</table>';
  }

  public function exportAction()
  {
    header('Content-type:application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename='. date('Y-m-d_G-i-s', time()) . '_玩家.xls');
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf8" />';
    echo '<table width="200" border="1">';
    echo '<tr><td>' . join('</td><td>', [
      '微信名', '姓名', '电话', '地址'
    ]) . '</td></tr>';
    $playerModel = $this->getModel('player');
    $query = [
      'opengift' => 0,
    ];
    $rows = 100;
    $page = 1;
    do {
      foreach (($list = $playerModel->getPlayersList($query, $page++, $rows)) as $player) {
        echo '<tr><td>' . join('</td><td style="vnd.ms-excel.numberformat:@ ">', [
          $player->nickname, $player->realname, $player->phone,
          $player->address
        ]) . '</td></tr>';
      }
    } while( count($list) == $rows);
    echo '</table>';
  }

  public function export30Action()
  {
    header('Content-type:application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename='. date('Y-m-d_G-i-s', time()) . '_排行榜.xls');
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf8" />';
    echo '<table width="200" border="1">';
    echo '<tr><td>' . join('</td><td>', [
      '微信名', '姓名', '电话', '地址'
    ]) . '</td></tr>';
    $playerModel = $this->getModel('player');
    $query = [
      // 'opengift' => 1,
      'flowers_history DESC' => true
    ];
    $rows = 30;
    $page = 1;
    foreach (($list = $playerModel->getPlayersList($query, $page++, $rows)) as $player) {
      echo '<tr><td>' . join('</td><td style="vnd.ms-excel.numberformat:@ ">', [
        $player->nickname, $player->realname, $player->phone,
        $player->address
      ]) . '</td></tr>';
    }
    echo '</table>';
  }

  public function flowerlogAction($playerid)
  {
    $playerModel = $this->getModel('player');
    if ($player = $playerModel->getPlayer($playerid)) {
      $page = max(intval($this->req->getQuery('page', 1)), 1);
      $rows = max(intval($this->req->getQuery('rows', 5)), 1);
      $query = [];
      return $this->okReturn([
       'list' => $player->getFlowerLogsList($query, $page, $rows),
       'count' => $player->getFlowerLogsCount($query),
       'page' => $page,
       'rows' => $rows
      ]);
    }
    return $this->okReturn([]);
  }

  public function commentAction()
  {
    $playerModel = $this->getModel('player');
    $view = $this->getView();
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $player = $playerModel->getPlayer($this->req->getQueryTrim('player_id'));
    $store = $player ? null: $playerModel->getPlayer($this->req->getQueryTrim('store_id'));
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'player_id' => $player->player_id ?? null,
      'store_id' => $store->player_id ?? null,
    ];
    $view = $this->getView();
    $list = $playerModel->getCommentsList($query, $page, $rows);
    $count = $playerModel->getCommentsCount($query);
    $view->assign([
     'query' => $query, 'player' => $player, 'store' => $store,
     'list' => $list, 'count' => $count,
     'page' => $page, 'rows' => $rows
   ]);
    $view->display('admin/player/comment.phtml');
  }

  public function commentUpdatedAction($op, $commentid)
  {
    $playerModel = $this->getModel('player');
    if (!($comment = $playerModel->getComment($commentid))) {
      $this->addErrorMessage('参数错误！！');
    } else if ($op == 'delete') {
      if ($comment->delete()) {
        $this->addSuccessMessage('删除留言[' . $comment->comment .  '] 成功');
      } else {
        $this->addErrorMessage('删除留言[' . $comment->comment .  '] 失败,请重试！！');
      }
    }
    $this->gotoUrl('admin/player/comment', true);
  }

  public function luckydrawAction()
  {
    $playerModel = $this->getModel('player');
    $view = $this->getView();
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $player = $playerModel->getPlayer($this->req->getQueryTrim('player_id'));
    $store = $player ? null: $playerModel->getPlayer($this->req->getQueryTrim('store_id'));
    $rows = 15;
    $query = [
    ];
    $view = $this->getView();
    $list = $playerModel->getLuckydrawsList($query, $page, $rows);
    $count = $playerModel->getLuckydrawsCount($query);
    $todaytime = strtotime(date('Y-m-d', REQUEST_TIME) . ' 0:00:00');
    $view->assign([
     'query' => $query,
     'list' => $list, 'count' => $count,
      'luckydrawpond' => $this->getModel('config')->get('luckydraw.pond', 0),
      'totaljoin' => $playerModel->getLuckydrawsCount([], true),
      'todayjoin' => $playerModel->getLuckydrawsCount([
        'created gte' => $todaytime,
      ], true),
      'yesterdayjoin' => $playerModel->getLuckydrawsCount([
        'created gte' => $todaytime - 86400,
        'created lte' => $todaytime,
      ], true),
     'page' => $page, 'rows' => $rows,
   ]);
    $view->display('admin/player/luckydraw.phtml');
  }

}
